import datetime
import geo.geomodel
import logging as log
import geo.geomath

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from django.utils import simplejson as json

class User (db.Model):
    
	username = db.StringProperty(required=True)
	rating = db.RatingProperty()
	location = db.GeoPtProperty()
	description = db.StringProperty()
	picture = db.StringProperty()
    
	@staticmethod
	def Create(username, description,picture,rating):
        
		user = User.get_or_insert(username,username=username)
		user.rating = int(rating)
		user.location=db.GeoPt(0,0)
		user.description = description
		user.picture = picture
		user.put()
				
class AddUserHandler(webapp.RequestHandler):
    """
    Description: Add new road POST request handler
    Params: name (string), path (multiple params allowed to send an array of coordinates)
    URL: /add_user?user=Ilan Arbiv&desc=Just Some Java Programmer&pic=http://www.isc.tamu.edu/~lewing/linux/sit3-shine.7.gif&rat=50
    """
    
    def get(self):
        
        # Check user is logged in
		username = self.request.get("user")
		description = self.request.get("desc")
		picture = self.request.get("pic")
		rating = self.request.get("rat")
       
		if username:
			User.Create(username, description,picture,rating)
		else:
			self.response.out.write("no username given")
			return 
		
class GetUserHandler(webapp.RequestHandler):
    """
    Description: Add new road POST request handler
    Params: name (string), path (multiple params allowed to send an array of coordinates)
    URL: /get_user?user=Ilan Arbiv
    """
    def get(self):
        
		username = self.request.get("user")
		user=None
		users = User.all().filter('username',username)
		if users:
			for user in users:
				json_user = {
				'user': user.username,
				'desc': user.description,
				'loc' : str(user.location.lat)+","+str(user.location.lon),
				'pic' : user.picture,
				'rat' : str(user.rating),
				}
		if user is None:
			self.response.out.write("no such user")
		else:
			self.response.out.write(json.dumps(json_user))